import { Brand } from '../Brand/Brand'
import {
    requestCategoryList,
    requestAddCategoryList,
    requestRemoveCategoryList,
    requestEditCategorylist
} from '../../api/category'
import { message } from 'antd'
import { connect } from 'react-redux'

class Classify extends Brand {
    btn = '添加分类'
    title = '添加/修改分类'
    placeholder = '请输入分类'

    init () {
        requestCategoryList().then(res => {
            this.setState({
                cardList: res.data,
            })
            setTimeout(() => {
                this.setState({
                    loading: false
                })
            }, 400)
        })
    }

    insert (category) {
        if(!category||!category.length){
            message.warn("请输入分类")
            return;
        }
        requestAddCategoryList(category).then(res => {
            if (res.code === 200) {
                message.success(res.msg)
            } else {
                message.warn(res.msg)
            }
            this.setState({
                isModalVisible: false,
            })
            this.init()
        })
    }

    edit (category) {
        const { _brand } = this.state
        if(!category||!category.length){
            message.warn("请输入分类")
            return;
        }
        requestEditCategorylist(_brand, category).then(res => {
            this.setState({
                isModalVisible: false,
            })
            message.success("修改分类成功")
            this.init()
        })
    }

    deleted (category) {
        requestRemoveCategoryList(category).then(res => {
            message.success(res.msg)
            this.setState({
                isModalVisible: false,
            })
            this.init()
        })
    }
}

export default connect((state) => {
    return {
        show: state.getIn(['fade', 'show'])
    }
})(Classify)
